/*
 * @lc app=leetcode.cn id=202 lang=javascript
 *
 * [202] 快乐数
 */

// @lc code=start
/**
 * @param {number} n
 * @return {boolean}
 */
var isHappy = function (n) {
  const getList = (num) =>
    num
      .toString()
      .split("")
      .map((item) => parseInt(item));
  const getSum = (list) => list.reduce((sum, cur) => sum + cur * cur, 0);

  let temp = getList(n);
  for (let i = 0; i < 99999; i++) {
    const res = getSum(temp);
    if (res === 1) {
      return true;
    }
    temp = getList(res);
  }

  return false;
};
// @lc code=end
